package com.fy56.platform.logistics.restful.api.controller.wx;

import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.common.ResultStatus;
import com.fy56.platform.logistics.model.News;
import com.fy56.platform.logistics.restful.api.app.authorization.annotation.Authorization;
import com.fy56.platform.logistics.service.web.INewsService;
import com.google.common.collect.Lists;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author by ct 2018-05-14
 */
@Api(value = "news", description = "消息类", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private INewsService iNewsServiceNewsService;

    private static Logger log = Logger.getLogger(NewsController.class);

    @RequestMapping(value = "/getNewsList", method = RequestMethod.POST)
    @ApiOperation(value = "获取最新消息")
    @Authorization
    public ResponseEntity getNewsList(@RequestParam String code){
        Long begin = System.currentTimeMillis();//开始时间
        List<Map<String,Object>> list = Lists.newArrayList();
        News news = new News();
        news.setCode(code);
        try {
            list = iNewsServiceNewsService.getHomeNewsList(news);
            Long end = System.currentTimeMillis();//结束时间
            log.info("getNewsList总用时：" + (end - begin) + "毫秒。");
            return new ResponseEntity(ResultModel.ok(list), HttpStatus.OK);
        } catch (Exception e) {
            log.error("获取最新消息失败");
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, "获取最新消息失败"), HttpStatus.OK);
        } finally {
            list = null;
            news = null;
        }

    }

}